/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CadastroCurso.java
 *
 * Created on 07/01/2003, 20:23:04
 */

package virtual.view;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import virtual.BD.Curso;
import virtual.service.CursoJpaController;
import virtual.service.exceptions.NonexistentEntityException;

/**
 *
 * @author Rafael
 */
public class CadastroCurso extends javax.swing.JPanel {
   private CursoJpaController service;

    /** Creates new form CadastroTurma */



    /** Creates new form CadastroCurso */
    public CadastroCurso() {
           service = new CursoJpaController();
        initComponents();
    }

    /** Creates new form CadastroCurso */
    public CadastroCurso(Curso curso) {
        initComponents();

        service = new CursoJpaController();

        this.preencherCurso(curso);

        
    } // Fim do construtor


    /**
     * Preenche os campos com um curso.
     * @param curso curso que preenche os campos.
     */
    public void preencherCurso(Curso curso) {

        jbExcluir.setVisible(true);
        this.jtfcodigoCurso.setText(curso.getCdCurso().toString());
        this.jtfNomeTurma.setText(curso.getNmCurso());


        this.jtfDuracaoCurso.setText(curso.getQtDuracaoCurso()+"");
        this.jtfValorCurso.setText(curso.getVlCurso()+"");

    } // Fim do método preencherTurma


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jtfcodigoCurso = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtfNomeTurma = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jtfDuracaoCurso = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jtfValorCurso = new javax.swing.JTextField();
        jbCadastroCurso = new javax.swing.JButton();
        jbCadastroCurso1 = new javax.swing.JButton();
        jbExcluir = new javax.swing.JButton();

        jtfcodigoCurso.setEditable(false);
        jtfcodigoCurso.setName("jtfcodigoCurso"); // NOI18N

        jLabel1.setText("Código:");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Nome: ");
        jLabel2.setName("jLabel2"); // NOI18N

        jtfNomeTurma.setName("jtfNomeTurma"); // NOI18N

        jLabel3.setText("Duração:");
        jLabel3.setName("jLabel3"); // NOI18N

        jtfDuracaoCurso.setName("jtfDuracaoCurso"); // NOI18N

        jLabel4.setText("Valor:");
        jLabel4.setName("jLabel4"); // NOI18N

        jtfValorCurso.setName("jtfValorCurso"); // NOI18N

        jbCadastroCurso.setText("Cadastrar");
        jbCadastroCurso.setName("jbCadastroCurso"); // NOI18N
        jbCadastroCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCadastroCursoActionPerformed(evt);
            }
        });

        jbCadastroCurso1.setText("Pesquisar");
        jbCadastroCurso1.setName("jbCadastroCurso1"); // NOI18N
        jbCadastroCurso1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCadastroCurso1ActionPerformed(evt);
            }
        });

        jbExcluir.setText("Excluir");
        jbExcluir.setName("jbExcluir"); // NOI18N
        jbExcluir.setVisible(false); //Ocultar o botão
        jbExcluir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbExcluirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtfcodigoCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfValorCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfDuracaoCurso, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtfNomeTurma, javax.swing.GroupLayout.PREFERRED_SIZE, 372, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(119, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(243, Short.MAX_VALUE)
                .addComponent(jbExcluir, javax.swing.GroupLayout.PREFERRED_SIZE, 82, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbCadastroCurso1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jbCadastroCurso)
                .addGap(40, 40, 40))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfcodigoCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfNomeTurma, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jtfDuracaoCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jtfValorCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(45, 45, 45)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbCadastroCurso)
                    .addComponent(jbCadastroCurso1)
                    .addComponent(jbExcluir))
                .addContainerGap(37, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jbCadastroCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCadastroCursoActionPerformed
try{

         Curso curso = new Curso();
         
         //curso.setCdCurso();
         curso.setNmCurso(jtfNomeTurma.getText());
         curso.setQtDuracaoCurso(Short.parseShort(jtfDuracaoCurso.getText()));
         curso.setVlCurso(Double.parseDouble(jtfValorCurso.getText()));

          if (this.jtfcodigoCurso.getText().equals("")) {
            service.create(curso);
            JOptionPane.showMessageDialog(this, curso.getNmCurso() + " foi cadastrado com sucesso!", "Salvar", JOptionPane.INFORMATION_MESSAGE);
        } else {
           // curso.setCdCurso(Integer.parseInt(this.jtfcodigoCurso.getText()));
            try {
                service.edit(curso);
            } catch (NonexistentEntityException nee) {
                Logger.getLogger(CadastroPessoa.class.getName()).log(Level.SEVERE, null, nee);
            } catch (Exception e) {
                Logger.getLogger(CadastroPessoa.class.getName()).log(Level.SEVERE, null, e);
            } // Fim do try/catch
        }//fim else
          jtfNomeTurma.setText("");
          jtfDuracaoCurso.setText("");
          jtfValorCurso.setText("");
          jtfcodigoCurso.setText(curso.getCdCurso().toString());
                        
}//fim try
             catch (Exception e){
        JOptionPane.showMessageDialog(this, "Não foi possível cadastrar.", "Erro ao cadastrar Curso!", JOptionPane.ERROR_MESSAGE);
        System.err.println("ERRO em CadastroCurso.jbCadastroCursoActionPerformed(java.awt.event.ActionEvent):\n" + e);
    }  // Fim do try/catch




    
    


    }//GEN-LAST:event_jbCadastroCursoActionPerformed

    private void jbCadastroCurso1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCadastroCurso1ActionPerformed
     PesquisaCurso pes = new PesquisaCurso();



    Principal.abreNovaJanelaInterna(pes);
    }//GEN-LAST:event_jbCadastroCurso1ActionPerformed

    private void jbExcluirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbExcluirActionPerformed
        try {

            service.destroy(Integer.parseInt(jtfcodigoCurso.getText()));

            jtfcodigoCurso.setText("");
            jtfNomeTurma.setText("");
            jtfValorCurso.setText("");
            jtfDuracaoCurso.setText("");

            jbExcluir.setVisible(false);

            JOptionPane.showMessageDialog(this, "Curso excluido com sucesso.");


        } catch (NonexistentEntityException ex) {
            Logger.getLogger(CadastroCurso.class.getName()).log(Level.SEVERE, null, ex);
        }

    }//GEN-LAST:event_jbExcluirActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton jbCadastroCurso;
    private javax.swing.JButton jbCadastroCurso1;
    private javax.swing.JButton jbExcluir;
    private javax.swing.JTextField jtfDuracaoCurso;
    private javax.swing.JTextField jtfNomeTurma;
    private javax.swing.JTextField jtfValorCurso;
    private javax.swing.JTextField jtfcodigoCurso;
    // End of variables declaration//GEN-END:variables

}